package com.zhuxulin.android.test.fragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zhuxulin.android.test.Infos;
import com.zhuxulin.android.test.R;
import com.zhuxulin.android.test.adapter.MainAdapter;

public class MainFragment extends Fragment {
	private ListView simples = null;
	private MainAdapter adapter = null;
	private List<String> list = null;

	public MainFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.f_main, container, false);
		simples = (ListView) rootView.findViewById(R.id.test_list_view);
		return rootView;
	}

	@Override
	public void onStart() {
		super.onStart();
		initList();
		adapter = new MainAdapter(getActivity(), list);
		simples.setAdapter(adapter);
		simples.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				startActivity(new Intent(((String) simples.getItemAtPosition(position))));
			}
		});
	}

	private void initList() {
		list = new ArrayList<String>();
		Map<String, Object> map = null;
		map = new HashMap<String, Object>();
		// list.add(Infos.ACTION_SLIST_VIEW);
		for (Field i : Infos.class.getDeclaredFields()) {
			try {
				list.add((String) i.get(null));
			} catch (IllegalAccessException | IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
	}
}